<?php

abstract class FieldText extends Field{
    
    public $size;
    public $maxLenght;
    
    public function __construct($name, $caption, $isRequired = false, $value = "", $maxLenght = "255", $size = 41, $parameters = "", $help = "") {
        parent::__construct($name, 'text', $caption, $isRequired, $value, $parameters, $help);

        $this->size = $size;
        $this->maxLenght = $maxLenght;
    }

    public function getHtml() {
        if (!empty($this->css_style)) {
            $style = 'style="' . $this->css_style . '"';
        }
        else
            $style = '';

        if (!empty($this->css_class)) {
            $class = 'class="' . $this->css_class . '"';
        }
        else
            $class = '';

        if (!empty($this->size))
            $size = 'size="' . $this->size . '"';
        else
            $size = '';

        if (!empty($this->maxLenght)) {
            $maxLenght = 'maxlenght="' . $this->maxLenght . '"';
        }
        else
            $maxLenght = '';

        $tag = '<input ' . $style . ' ' . $class . ' type="' . $this->type . '" name="' . $this->name . '" value="' . htmlspecialchars($this->name, ENT_QUOTES) . '" ' . $size . ' ' . $maxLenght . '';

        if ($this->isRequired)
            $this->caption.='*';

        $help = '';
        if (!empty($this->help)) {
            $help .= '<span style="color:blue">' .
                    nl2br($this->help) . '</span>';
        }

        if (!empty($help))
            $help .= '<br>';

        return array($this->caption, $tag, $help);
    }

    function check() {

        if ($this->is_required) {
            if (empty($this->value)) {
                return 'Поле "' . $this->caption . '" не заполнено';
            }
        }

        return '';
    }

}
?>
